<?php
namespace Admin\Controller;

class AreaController extends BaseController
{
    public function indexAction()
    {
        $pid = I('pid', 0, 'intval');
        $Area = M('Area');
        $condition = array(
            'pid' => $pid,
        );
        $list = $Area->where($condition)->order('sort asc, id asc')->select();
        $this->assign('area_list', $list);
        $this->assign('pid', $pid);
        $this->show();
    }
    
    public function addAction()
    {
        $pid = I('get.pid', 0, 'intval');
        $Area = D('Area');
        if (IS_POST) {
            $data = $Area->create($_POST['Area']);
            if (false !== $data) {
                $insertId = $Area->add();
                if (false !== $insertId) {
                    $this->success('新增成功', U('Area/index', array('pid' => $pid)));
                } else {
                    $this->error('新增失败');
                }
            } else {
                $this->error($Area->getError());
            }
        } else {
            $parent_area_info = array();
            if ($pid != 0) {
                $parent_area_info = $Area->where("id=" . $pid)->find();
            }
            $level = !empty($parent_area_info) ? $parent_area_info['level'] + 1 : 1;
            $this->assign('parent_area_info', $parent_area_info);
            $this->assign('level', $level);
            $this->assign('pid', $pid);
            $this->display();  
        }
    }
    
    public function updateAction()
    {
        $pid = I('get.pid', 0, 'intval');
        $Area = D('Area');
        if (IS_POST) {
            $data = $Area->create($_POST['Area']);
            if (false !== $data) {
                if (false !== $Area->save()) {
                    $this->success('更新成功', U('Area/index', array('pid' => $pid)));
                } else {
                    $this->error('更新失败（失败原因：' . $Area->getError() . '）');
                }
            } else {
                $this->error($Area->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $area_info = $Area->find($id);
            if (false === $area_info) {
                $this->error('获取信息失败！');
            }
            
            $parent_area_info = array();
            if ($area_info['pid'] != 0) {
                $parent_area_info = $Area->where("id=" . $area_info['pid'])->find();
            }
            $this->assign('parent_area_info', $parent_area_info);
            $this->assign('area_info', $area_info);
            $this->assign('pid', $pid);
            $this->display();  
        }
    }
    
    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $Area = M('Area');
        $list = $Area->where(array('pid' => $id))->select();
        if (!empty($list)) {
            $this->error('该地区下有子地区，请先删除子地区！');
        }
        
        $res = $Area->delete($id);
        if (false !== $res) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    
	/**
     * 编辑器上传处理
     */
	public function uploadJsonAction() 
    {
		$config = array(
			'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'area/',
            'subPath' => 'area/',
			'saveUrl' => C('PICTURE_UPLOAD_URL'),
		);
        vendor('Kindeditor.UploadJson', '', '.class.php');
		$UploadJson = new \UploadJson($config);
		$UploadJson->upload();
	}
    
	/**
     * 编辑器上传文件管理
     */
	public function fileManagerJson()
    {
		
	}
}